package com.zian.controller.admin;


import com.zian.annotation.OperLog;
import com.zian.common.R;
import com.zian.domain.dto.IndexConfigDTO;
import com.zian.domain.dto.PageDTO;
import com.zian.domain.po.SysIndexConfig;
import com.zian.domain.query.IndexConfigQuery;
import com.zian.domain.vo.IndexConfigQueryVO;
import com.zian.domain.vo.IndexConfigVO;
import com.zian.enums.BusinessType;
import com.zian.exception.BadRequestException;
import com.zian.exception.BizIllegalException;
import com.zian.exception.DbException;
import com.zian.service.ISysIndexConfigService;
import com.zian.validation.ValidGroup;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author
 * @since 2024-09-08
 */
@Slf4j
@Api("首页推荐配置管理")
@RestController("SysIndexConfigControllerAdmin")
@RequestMapping("/admin/index-configs")
@RequiredArgsConstructor
public class SysIndexConfigController {

    private final ISysIndexConfigService indexConfigService;


    @GetMapping("/page")
    @ApiOperation("分页获取首页配置列表")
    public R<PageDTO<IndexConfigQueryVO>> queryIndexConfigList(IndexConfigQuery query) {
        log.info("分页查询 ：{}", query);
        PageDTO<IndexConfigQueryVO> dto = indexConfigService.queryIndexConfigList(query);
        return R.ok(dto);
    }


    @DeleteMapping
    @ApiOperation("删除首页配置")
    @OperLog(title = "首页配置", businessType = BusinessType.DELETE)
    public R deleteIndexConfigByIds(@RequestParam("ids") List<Long> ids) {
        log.info("删除首页配置：{}", ids);
        indexConfigService.deleteIndexConfigByIds(ids);
        return R.ok();
    }


    @PostMapping
    @ApiOperation("新增首页配置")
    @OperLog(title = "首页配置", businessType = BusinessType.INSERT)
    public R saveIndexConfig(@RequestBody @Validated(ValidGroup.Crud.Create.class) IndexConfigDTO indexConfigDTO) {
        log.info("新增首页配置：{}", indexConfigDTO);
        indexConfigService.saveIndexConfig(indexConfigDTO);
        return R.ok();
    }

    @PutMapping
    @ApiOperation("修改首页配置")
    @OperLog(title = "首页配置", businessType = BusinessType.UPDATE)
    public R updateIndexConfig(@RequestBody @Validated(ValidGroup.Crud.Update.class) IndexConfigDTO indexConfigDTO) {
        log.info("修改首页配置：{}", indexConfigDTO);
        indexConfigService.updateIndexConfig(indexConfigDTO);
        return R.ok();
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询首页配置")
    public R<SysIndexConfig> getIndexConfigById(@PathVariable("id") Long id) {
        log.info("根据id查询首页配置：{}", id);
        if (id == null) throw new BadRequestException("id 不能为空");
        SysIndexConfig indexConfigDTO = indexConfigService.getById(id);
        if (indexConfigDTO == null) throw new BizIllegalException("配置不存在");
        return R.ok(indexConfigDTO);
    }


    @GetMapping("/list")
    @ApiOperation("查询首页配置列表")
    public R<List<IndexConfigVO>> getIndexConfigList(Integer type) {
        log.info("查询首页配置列表 : 查询类型：{}", type);
        List<IndexConfigVO> vos = indexConfigService.getIndexConfigList(type);
        return R.ok(vos);
    }

}
